//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "PatternsCathFASTAProfile.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <stdexcept>
#include <iostream>
#include <fstream>

#include <zlib.h>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHLINE           100000

#define CUOTE_ARFF           "\""

#define COMMENT_ARFF         "% "
#define RELATION_ARFF        "@relation "
#define DOMAIN_ATT_ARFF      "@attribute domain STRING"
#define PATTERN_ATT_ARFF     "@attribute \"%-s\" INTEGER\0"
#define PATTERN_ATTN_ARFF    "@attribute \"%-s\" {yes, no}\0"
#define CLASS_ATT_ARFF       "@attribute class {bind, not-bind}"
#define DATA_ARFF            "@data"
#define VALUE_YES_ARFF       ",yes" 
#define VALUE_NO_ARFF        ",no" 
#define VALUE_BIND_ARFF      ",bind" 
#define VALUE_NBIND_ARFF     ",not-bind"

#define ATTRIBUTE_COMPLETE   1
#define PRINT_OUT            0
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
namespace PatternSpace {

  bool EqualDomain (TNode <CathFASTAProfile>* cathFASTAProfile, void* ref) {

    return (!strcmp(cathFASTAProfile->Info()->Domain()->Out(), ((String*)ref)->Out()))?true:false;

  }
  
}  
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PatternsCathFASTAProfile::PatternsCathFASTAProfile () {
  
  this->filenameCathFASTAProfile        = new String ();
  this->filenameLigandCathFASTAProfile  = new String ();
  this->filenameExcludeCathFASTAProfile = new String ();
  this->filenamePatternsCompact         = new String ();
  this->filenameLigandFASTAProfile      = new String ();
  this->filenameSMI                     = new String ();
  this->filenameOut                     = new String ();
  
  this->prattPatterns                   = new Pratt2PatternsByCATH ();
  this->cathFASTAProfile                = new ProcessCathFASTAProfile ();
  this->ligandCathFASTAProfile          = new ProcessCathFASTAProfile ();
  this->excludeCathFASTAProfile         = new ProcessCathFASTAProfile ();
  this->searchPattern                   = new SearchPrositePattern ();

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PatternsCathFASTAProfile::PatternsCathFASTAProfile (const PatternsCathFASTAProfile& patternsCathFASTAProfile) {
  
  this->filenameCathFASTAProfile        = new String (patternsCathFASTAProfile.filenameCathFASTAProfile);
  this->filenameLigandCathFASTAProfile  = new String (patternsCathFASTAProfile.filenameLigandCathFASTAProfile);
  this->filenameExcludeCathFASTAProfile = new String (patternsCathFASTAProfile.filenameExcludeCathFASTAProfile);
  this->filenamePatternsCompact         = new String (patternsCathFASTAProfile.filenamePatternsCompact);
  this->filenameLigandFASTAProfile      = new String (patternsCathFASTAProfile.filenameLigandFASTAProfile);
  this->filenameSMI                     = new String (patternsCathFASTAProfile.filenameSMI);
  this->filenameOut                     = new String (patternsCathFASTAProfile.filenameOut);
  
  this->prattPatterns                   = new Pratt2PatternsByCATH (patternsCathFASTAProfile.prattPatterns);
  this->cathFASTAProfile                = new ProcessCathFASTAProfile (patternsCathFASTAProfile.cathFASTAProfile);
  this->ligandCathFASTAProfile          = new ProcessCathFASTAProfile (patternsCathFASTAProfile.ligandCathFASTAProfile);
  this->excludeCathFASTAProfile         = new ProcessCathFASTAProfile (patternsCathFASTAProfile.excludeCathFASTAProfile);
  this->searchPattern                   = new SearchPrositePattern (patternsCathFASTAProfile.searchPattern);

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PatternsCathFASTAProfile::PatternsCathFASTAProfile (const PatternsCathFASTAProfile* patternsCathFASTAProfile) {
  
  this->filenameCathFASTAProfile        = new String (patternsCathFASTAProfile->filenameCathFASTAProfile);
  this->filenameLigandCathFASTAProfile  = new String (patternsCathFASTAProfile->filenameLigandCathFASTAProfile);
  this->filenameExcludeCathFASTAProfile = new String (patternsCathFASTAProfile->filenameExcludeCathFASTAProfile);
  this->filenamePatternsCompact         = new String (patternsCathFASTAProfile->filenamePatternsCompact);
  this->filenameLigandFASTAProfile      = new String (patternsCathFASTAProfile->filenameLigandFASTAProfile);
  this->filenameSMI                     = new String (patternsCathFASTAProfile->filenameSMI);
  this->filenameOut                     = new String (patternsCathFASTAProfile->filenameOut);
  
  this->prattPatterns                   = new Pratt2PatternsByCATH (patternsCathFASTAProfile->prattPatterns);
  this->cathFASTAProfile                = new ProcessCathFASTAProfile (patternsCathFASTAProfile->cathFASTAProfile);
  this->ligandCathFASTAProfile          = new ProcessCathFASTAProfile (patternsCathFASTAProfile->ligandCathFASTAProfile);
  this->excludeCathFASTAProfile         = new ProcessCathFASTAProfile (patternsCathFASTAProfile->excludeCathFASTAProfile);
  this->searchPattern                   = new SearchPrositePattern (patternsCathFASTAProfile->searchPattern);

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PatternsCathFASTAProfile::~PatternsCathFASTAProfile () {
  
  if (this->filenameCathFASTAProfile)        delete this->filenameCathFASTAProfile;
  if (this->filenameLigandCathFASTAProfile)  delete this->filenameLigandCathFASTAProfile;
  if (this->filenameExcludeCathFASTAProfile) delete this->filenameExcludeCathFASTAProfile;
  if (this->filenamePatternsCompact)         delete this->filenamePatternsCompact;
  if (this->filenameLigandFASTAProfile)      delete this->filenameLigandFASTAProfile;
  if (this->filenameSMI)                     delete this->filenameSMI;
  if (this->filenameOut)                     delete this->filenameOut;
  
  if (this->prattPatterns)                   delete this->prattPatterns;
  if (this->cathFASTAProfile)                delete this->cathFASTAProfile;
  if (this->ligandCathFASTAProfile)          delete this->ligandCathFASTAProfile;
  if (this->excludeCathFASTAProfile)         delete this->excludeCathFASTAProfile;
  if (this->searchPattern)                   delete this->searchPattern;
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PatternsCathFASTAProfile::FilenameCathFASTAProfile (String* filenameCathFASTAProfile) {
  
  if (filenameCathFASTAProfile) this->filenameCathFASTAProfile->In(filenameCathFASTAProfile);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PatternsCathFASTAProfile::FilenameLigandCathFASTAProfile (String* filenameLigandCathFASTAProfile) {
  
  if (filenameLigandCathFASTAProfile) this->filenameLigandCathFASTAProfile->In(filenameLigandCathFASTAProfile);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PatternsCathFASTAProfile::FilenameExcludeCathFASTAProfile (String* filenameExcludeCathFASTAProfile) {
  
  if (filenameExcludeCathFASTAProfile) this->filenameExcludeCathFASTAProfile->In(filenameExcludeCathFASTAProfile);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PatternsCathFASTAProfile::FilenamePatternsCompact (String* filenamePatternsCompact) {
  
  if (filenamePatternsCompact) this->filenamePatternsCompact->In(filenamePatternsCompact);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PatternsCathFASTAProfile::FilenameLigandFASTAProfile (String* filenameLigandFASTAProfile) {
  
  if (filenameLigandFASTAProfile) this->filenameLigandFASTAProfile->In(filenameLigandFASTAProfile);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PatternsCathFASTAProfile::FilenameSMI (String* filenameSMI) {
  
  if (filenameSMI) this->filenameSMI->In(filenameSMI);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PatternsCathFASTAProfile::FilenameOut (String* filenameOut) {
  
  if (filenameOut) this->filenameOut->In(filenameOut);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PatternsCathFASTAProfile::PrattPatterns (Pratt2PatternsByCATH* prattPatterns) {
  
  if (prattPatterns) *(this->prattPatterns) = *prattPatterns;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PatternsCathFASTAProfile::CathProfile (ProcessCathFASTAProfile* cathFASTAProfile) {
  
  if (cathFASTAProfile) *(this->cathFASTAProfile) = *cathFASTAProfile;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PatternsCathFASTAProfile::LigandCathFASTAProfile (ProcessCathFASTAProfile* ligandCathFASTAProfile) {
  
  if (ligandCathFASTAProfile) *(this->ligandCathFASTAProfile) = *ligandCathFASTAProfile;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PatternsCathFASTAProfile::ExcludeCathFASTAProfile (ProcessCathFASTAProfile* excludeCathFASTAProfile) {
  
  if (excludeCathFASTAProfile) *(this->excludeCathFASTAProfile) = *excludeCathFASTAProfile;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PatternsCathFASTAProfile::SearchPattern (SearchPrositePattern* searchPattern) {
  
  if (searchPattern) *(this->searchPattern) = *searchPattern;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* PatternsCathFASTAProfile::FilenameCathFASTAProfile (void) {
  
  return this->filenameCathFASTAProfile;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* PatternsCathFASTAProfile::FilenameLigandCathFASTAProfile (void) {
  
  return this->filenameLigandCathFASTAProfile;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* PatternsCathFASTAProfile::FilenameExcludeCathFASTAProfile (void) {
  
  return this->filenameExcludeCathFASTAProfile;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* PatternsCathFASTAProfile::FilenamePatternsCompact (void) {
  
  return this->filenamePatternsCompact;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* PatternsCathFASTAProfile::FilenameLigandFASTAProfile (void) {
  
  return this->filenameLigandFASTAProfile;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* PatternsCathFASTAProfile::FilenameSMI (void) {
  
  return this->filenameSMI;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* PatternsCathFASTAProfile::FilenameOut (void) {
  
  return this->filenameOut;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
Pratt2PatternsByCATH* PatternsCathFASTAProfile::PrattPatterns (void) {
  
  return this->prattPatterns;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ProcessCathFASTAProfile* PatternsCathFASTAProfile::CathProfile (void) {
  
  return this->cathFASTAProfile;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ProcessCathFASTAProfile* PatternsCathFASTAProfile::LigandCathFASTAProfile (void) {
  
  return this->ligandCathFASTAProfile;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ProcessCathFASTAProfile* PatternsCathFASTAProfile::ExcludeCathFASTAProfile (void) {
  
  return this->excludeCathFASTAProfile;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
SearchPrositePattern* PatternsCathFASTAProfile::SearchPattern (void) {
  
  return this->searchPattern;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PatternsCathFASTAProfile::LoadData (void) {
  
  if (!this->filenameCathFASTAProfile       || !this->filenameCathFASTAProfile->Length()       || 
      !this->filenameLigandCathFASTAProfile || !this->filenameLigandCathFASTAProfile->Length() || 
      !this->filenamePatternsCompact        || !this->filenamePatternsCompact->Length()        ||
      !this->cathFASTAProfile               || !this->ligandCathFASTAProfile                   || 
      !this->prattPatterns)
    return false;
  
  this->cathFASTAProfile->NamefileIn(this->filenameCathFASTAProfile);
  this->ligandCathFASTAProfile->NamefileIn(this->filenameLigandCathFASTAProfile);
  this->prattPatterns->Namefile(this->filenamePatternsCompact);
  
  this->cathFASTAProfile->LoadData();
  this->ligandCathFASTAProfile->LoadData();
  this->prattPatterns->LoadData();
  
  if (this->filenameExcludeCathFASTAProfile && this->filenameExcludeCathFASTAProfile->Length()) {
    this->excludeCathFASTAProfile->NamefileIn(this->filenameExcludeCathFASTAProfile);
    this->excludeCathFASTAProfile->LoadData();
  }
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PatternsCathFASTAProfile::ComputeProbPatternBelongToRightGroup (String* category) {
  
  if (!this->cathFASTAProfile || !this->ligandCathFASTAProfile || !this->prattPatterns)
    return false;
  
  this->prattPatterns->ListGroupPrattPatterns()->SetInitial();
  while (this->prattPatterns->ListGroupPrattPatterns()->SetNext()) {
    
    Pratt2Patterns* currentPratt2Patterns = this->prattPatterns->ListGroupPrattPatterns()->GetCurrent();
    
    cout << "Processing the patterns obtained with the parameters: " << endl;
    cout << currentPratt2Patterns->ToStringParameters()->Out() << endl;
    
    currentPratt2Patterns->Patterns()->SetInitial();
    while (currentPratt2Patterns->Patterns()->SetNext()) {
      
      PatternRecord* currentPatternRecord = currentPratt2Patterns->Patterns()->GetCurrent();
      
      cout << endl << "Pattern : " << currentPatternRecord->Pattern()->Out() << endl;
      
      this->searchPattern->Pattern(currentPatternRecord->Pattern());
      
      long numberOfSequenceContainAPattern              = 0;
      long numberOfDomainBindLingandThatContainAPattern = 0;
      
      cout << endl << "(1) Searching the matches of the pattern in the dataset of domains that bind the ligand" << endl;
      this->ligandCathFASTAProfile->ListCathDomainProfile()->SetInitial();
      while (this->ligandCathFASTAProfile->ListCathDomainProfile()->SetNext()) {
        
        String* currentSequence = NULL;
        
        if      (!strcmp(category->Out(), "AA")) currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
        else if (!strcmp(category->Out(), "SS")) currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->SecondaryStructureP();
        else if (!strcmp(category->Out(), "CH")) currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassChemical();
        else if (!strcmp(category->Out(), "PH")) currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassPhysical();
        else if (!strcmp(category->Out(), "MT")) currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesMT();
        else                                     currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
        
        this->searchPattern->Sequence(currentSequence);
        this->searchPattern->SearchPattern();
        
        if (this->searchPattern->Segments()->Length()) 
          cout << ++numberOfDomainBindLingandThatContainAPattern  << " - Match (1): " << this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->CathCode()->Out() << "                                  \r";
        
      }
      
      cout << endl << endl << "(2) Searching the matches of the pattern in the complete dataset of domains" << endl;
      if (!numberOfDomainBindLingandThatContainAPattern) { currentPatternRecord->Probability(0.0); continue; }
      
      this->cathFASTAProfile->ListCathDomainProfile()->SetInitial();
      while (this->cathFASTAProfile->ListCathDomainProfile()->SetNext()) {
        
        String* currentSequence = NULL;
        
        if      (!strcmp(category->Out(), "AA")) currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
        else if (!strcmp(category->Out(), "SS")) currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->SecondaryStructureP();
        else if (!strcmp(category->Out(), "CH")) currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassChemical();
        else if (!strcmp(category->Out(), "PH")) currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassPhysical();
        else if (!strcmp(category->Out(), "MT")) currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesMT();
        else                                     currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
        
        this->searchPattern->Sequence(currentSequence);
        this->searchPattern->SearchPattern();
        
        if (this->searchPattern->Segments()->Length()) 
          cout << ++numberOfSequenceContainAPattern  << " - Match (2): " << this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->CathCode()->Out()  << "                                  \r";;
        
      }
      
      if (numberOfSequenceContainAPattern != 0)
        currentPatternRecord->Probability((double)numberOfDomainBindLingandThatContainAPattern/(double)numberOfSequenceContainAPattern);
      else
        currentPatternRecord->Probability(0.0);
      
      cout << endl << endl << "Conditional Probability = " << currentPatternRecord->Probability() << endl;
      
    }
    
  }
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PatternsCathFASTAProfile::ComputeAverageLINGOSimLigandsFoundByPattern (String* category) {
  
  if (!this->cathFASTAProfile || !this->ligandCathFASTAProfile || !this->prattPatterns || !this->filenameLigandFASTAProfile || !this->filenameSMI)
    return false;
  
  this->prattPatterns->ListGroupPrattPatterns()->SetInitial();
  while (this->prattPatterns->ListGroupPrattPatterns()->SetNext()) {
    
    Pratt2Patterns* currentPratt2Patterns = this->prattPatterns->ListGroupPrattPatterns()->GetCurrent();
    
    cout << "Processing the patterns obtained with the parameters: " << endl;
    cout << currentPratt2Patterns->ToStringParameters()->Out() << endl;
    
    currentPratt2Patterns->Patterns()->SetInitial();
    while (currentPratt2Patterns->Patterns()->SetNext()) {
      
      PatternRecord* currentPatternRecord = currentPratt2Patterns->Patterns()->GetCurrent();
      
      cout << endl << "Pattern : " << currentPatternRecord->Pattern()->Out() << endl;
      
      this->searchPattern->Pattern(currentPatternRecord->Pattern());
      
      long numberOfSequenceContainAPattern = 0;
      
      cout << endl << endl << "(1) Searching the matches of the pattern in the complete dataset of domains" << endl;
      
      this->cathFASTAProfile->ListCathDomainProfile()->SetInitial();
      while (this->cathFASTAProfile->ListCathDomainProfile()->SetNext()) {
        
        String* currentSequence = NULL;
        
        if      (!strcmp(category->Out(), "AA")) currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
        else if (!strcmp(category->Out(), "SS")) currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->SecondaryStructureP();
        else if (!strcmp(category->Out(), "CH")) currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassChemical();
        else if (!strcmp(category->Out(), "PH")) currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassPhysical();
        else if (!strcmp(category->Out(), "MT")) currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesMT();
        else                                     currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
        
        this->searchPattern->Sequence(currentSequence);
        this->searchPattern->SearchPattern();
        
        if (this->searchPattern->Segments()->Length()) {
          
          
          this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->CathCode()->Out();
          
          
        }
        
      }
      
//      if (numberOfSequenceContainAPattern != 0)
//        currentPatternRecord->Weight((double)numberOfDomainBindLingandThatContainAPattern/(double)numberOfSequenceContainAPattern);
//      else
//        currentPatternRecord->Weight(0.0);
      
      cout << endl << endl << "Average of LINGOSim = " << currentPatternRecord->Weight() << endl;
      
    }
    
  }
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PatternsCathFASTAProfile::CreateARFF (String* nameRelation, String* comments, int positionInPatternFile, float percentagePattern, float percentageFalse, String* typeOfData) {
  
  if (!this->cathFASTAProfile || !this->ligandCathFASTAProfile || !this->prattPatterns || !this->filenameOut || !this->filenameOut->Length() || 
      (positionInPatternFile < 0) || (positionInPatternFile > this->prattPatterns->ListGroupPrattPatterns()->Length()))
    return false;
  
  ofstream fileARFF (this->filenameOut->Out());
  
  if (!fileARFF.is_open()) return false;
  
  char     buffer [LENGTHLINE + 1];
  char*    parts = NULL;
  
  
  strcpy (buffer, comments->Out());
  
  parts = strtok(buffer, "\n\r");
  while (parts) {
    fileARFF << COMMENT_ARFF << parts << endl;
    if (PRINT_OUT) 
      cout     << COMMENT_ARFF << parts << endl;
    parts = strtok(NULL, "\n\r");
  }
  
  fileARFF << endl << RELATION_ARFF << CUOTE_ARFF << nameRelation->Out() << CUOTE_ARFF << endl << endl;
  if (PRINT_OUT) 
    cout     << endl << RELATION_ARFF << CUOTE_ARFF << nameRelation->Out() << CUOTE_ARFF << endl << endl;
  
  int             numberAttributes      = (ATTRIBUTE_COMPLETE)?
                                          (int) (this->prattPatterns->ListGroupPrattPatterns()->Get(positionInPatternFile - 1)->Patterns()->Length()*percentagePattern/100):
                                          (int) (this->ligandCathFASTAProfile->ListCathDomainProfile()->Length()*percentagePattern/100);
  int             counter               = 0;
  
  fileARFF << DOMAIN_ATT_ARFF << endl;
  if (PRINT_OUT) 
    cout     << DOMAIN_ATT_ARFF << endl;
  
  Pratt2Patterns* currentPratt2Patterns = this->prattPatterns->ListGroupPrattPatterns()->Get(positionInPatternFile - 1);
  
  currentPratt2Patterns->Patterns()->SetInitial();
  while (currentPratt2Patterns->Patterns()->SetNext() && counter++ < numberAttributes) {
    sprintf(buffer, PATTERN_ATT_ARFF, currentPratt2Patterns->Patterns()->GetCurrent()->Pattern()->Out());
    fileARFF << buffer << endl;
    if (PRINT_OUT) 
      cout     << buffer << endl;
  }
  
  fileARFF << CLASS_ATT_ARFF << endl;
  if (PRINT_OUT) 
    cout     << CLASS_ATT_ARFF << endl;
  
  fileARFF << endl << DATA_ARFF << endl << endl;
  if (PRINT_OUT) 
    cout     << endl << DATA_ARFF << endl << endl;
  
  this->ligandCathFASTAProfile->ListCathDomainProfile()->SetInitial();
  while (this->ligandCathFASTAProfile->ListCathDomainProfile()->SetNext()) {

    String* currentSequence = NULL;
    
    if      (*typeOfData == "Seq") currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
    else if (*typeOfData == "CH")  currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassChemical();
    else if (*typeOfData == "PH")  currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassPhysical();
    else if (*typeOfData == "SS")  currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->SecondaryStructureP();
    else if (*typeOfData == "MT")  currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesMT();
    else                          currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();

    this->searchPattern->Sequence(currentSequence);
    
    fileARFF << this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain()->Out();
    if (PRINT_OUT) 
      cout     << this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain()->Out();

    counter = 0;
    
    currentPratt2Patterns->Patterns()->SetInitial();
    while (currentPratt2Patterns->Patterns()->SetNext() && counter++ < numberAttributes) {
      
      this->searchPattern->Pattern(currentPratt2Patterns->Patterns()->GetCurrent()->Pattern());
      this->searchPattern->SearchPattern();
      
      fileARFF << "," << this->searchPattern->Segments()->Length();
      if (PRINT_OUT) 
        cout     << "," << this->searchPattern->Segments()->Length();

    }
    
    fileARFF << VALUE_BIND_ARFF << endl;
    if (PRINT_OUT) 
      cout     << VALUE_BIND_ARFF << endl;

  }
  
  int numberFalseData = ((percentageFalse >= 0 && percentageFalse <= 100)?((int) (this->ligandCathFASTAProfile->ListCathDomainProfile()->Length()*percentageFalse/(100 - percentageFalse))):(abs(percentageFalse)));
  int counterFalse    = 0;
  
  TListE <int>* listPosition  = new TListE <int> ();
  int           numberElemDel = 0;
  
  numberElemDel = this->cathFASTAProfile->ListCathDomainProfile()->Length() - (numberFalseData + this->ligandCathFASTAProfile->ListCathDomainProfile()->Length());
  
  if (numberElemDel < 0) {
    
    fileARFF << "ERROR!" << endl;
    cout     << "ERROR!" << endl;
     
    fileARFF.close();
    
    if (listPosition) delete listPosition;
    
    return false;
    
  }
    
  for (long i = 0; i < this->cathFASTAProfile->ListCathDomainProfile()->Length(); i++) 
    listPosition->Add(new int (i + 1));
  
  for (long i = 0; i < numberElemDel; i++) 
    listPosition->Delete(rand() % listPosition->Length());
  
  listPosition->SetInitial();
  if (!listPosition->SetNext()) {
    
    fileARFF << "ERROR!" << endl;
    cout     << "ERROR!" << endl;
     
    fileARFF.close();
    
    if (listPosition) delete listPosition;
    
    return false;
    
  }
    
  long position = 0;
    
  this->cathFASTAProfile->ListCathDomainProfile()->SetInitial();
  while (this->cathFASTAProfile->ListCathDomainProfile()->SetNext() && counterFalse < numberFalseData) {

    if (*(listPosition->GetCurrent()) != ++position) continue;
    
    CathFASTAProfile* searchCathFASTAProfile = this->ligandCathFASTAProfile->ListCathDomainProfile()->FirstOcurrence(PatternSpace::EqualDomain, this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain());

    if (searchCathFASTAProfile) { 
      if (!listPosition->SetNext()) 
        break;
      continue;
    }
    
    if (this->excludeCathFASTAProfile && this->excludeCathFASTAProfile->ListCathDomainProfile()->Length()) {
      
      searchCathFASTAProfile = this->excludeCathFASTAProfile->ListCathDomainProfile()->FirstOcurrence(PatternSpace::EqualDomain, this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain());

      if (searchCathFASTAProfile) { 
        if (!listPosition->SetNext()) 
          break;
        continue;
      }
      
    }
    
    counterFalse++;
    
    String* currentSequence = NULL;
    
    if      (*typeOfData == "Seq") currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
    else if (*typeOfData == "CH")  currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassChemical();
    else if (*typeOfData == "PH")  currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassPhysical();
    else if (*typeOfData == "SS")  currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->SecondaryStructureP();
    else if (*typeOfData == "MT")  currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesMT();
    else                           currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
    
    this->searchPattern->Sequence(currentSequence);
    
    fileARFF << this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain()->Out();
    if (PRINT_OUT) 
      cout     << this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain()->Out();

    counter = 0;
    
    currentPratt2Patterns->Patterns()->SetInitial();
    while (currentPratt2Patterns->Patterns()->SetNext() && counter++ < numberAttributes) {
      
      this->searchPattern->Pattern(currentPratt2Patterns->Patterns()->GetCurrent()->Pattern());
      this->searchPattern->SearchPattern();
      
      fileARFF << "," << this->searchPattern->Segments()->Length();
      if (PRINT_OUT) 
        cout     << "," << this->searchPattern->Segments()->Length();

    }
    
    fileARFF << VALUE_NBIND_ARFF << endl;
    if (PRINT_OUT) 
      cout     << VALUE_NBIND_ARFF << endl;

    if (!listPosition->SetNext()) break;
    
  }
  
  fileARFF.close();
  
  if (listPosition) delete listPosition;
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PatternsCathFASTAProfile::CreateARFFNominal (String* nameRelation, String* comments, int positionInPatternFile, float percentagePattern, float percentageFalse, String* typeOfData) {
  
  if (!this->cathFASTAProfile || !this->ligandCathFASTAProfile || !this->prattPatterns || !this->filenameOut || !this->filenameOut->Length() || 
      (positionInPatternFile < 0) || (positionInPatternFile >= this->prattPatterns->ListGroupPrattPatterns()->Length()))
    return false;
  
  ofstream fileARFF (this->filenameOut->Out());
  
  if (!fileARFF.is_open()) return false;
  
  char     buffer [LENGTHLINE + 1];
  char*    parts = NULL;
  
  strcpy (buffer, comments->Out());
  
  parts = strtok(buffer, "\n\r");
  while (parts) {
    fileARFF << COMMENT_ARFF << parts << endl;
    if (PRINT_OUT) 
      cout     << COMMENT_ARFF << parts << endl;
    parts = strtok(NULL, "\n\r");
  }
  
  fileARFF << endl << RELATION_ARFF << CUOTE_ARFF << nameRelation->Out() << CUOTE_ARFF << endl << endl;
  if (PRINT_OUT) 
    cout << endl << RELATION_ARFF << CUOTE_ARFF << nameRelation->Out() << CUOTE_ARFF << endl << endl;
  
  int             numberAttributes      = (ATTRIBUTE_COMPLETE)?
                                          (int) (this->prattPatterns->ListGroupPrattPatterns()->Get(positionInPatternFile - 1)->Patterns()->Length()*percentagePattern/100):
                                          (int) (this->ligandCathFASTAProfile->ListCathDomainProfile()->Length()*percentagePattern/100);
  
  int             counter               = 0;
  
  fileARFF << DOMAIN_ATT_ARFF << endl;
  if (PRINT_OUT) 
    cout     << DOMAIN_ATT_ARFF << endl;
  
  Pratt2Patterns* currentPratt2Patterns = this->prattPatterns->ListGroupPrattPatterns()->Get(positionInPatternFile - 1);
  
  currentPratt2Patterns->Patterns()->SetInitial();
  while (currentPratt2Patterns->Patterns()->SetNext() && counter++ < numberAttributes) {
    sprintf(buffer, PATTERN_ATTN_ARFF, currentPratt2Patterns->Patterns()->GetCurrent()->Pattern()->Out());
    fileARFF << buffer << endl;
    if (PRINT_OUT) 
      cout     << buffer << endl;
  }
  
  fileARFF << CLASS_ATT_ARFF << endl;
  if (PRINT_OUT) 
    cout     << CLASS_ATT_ARFF << endl;
  
  fileARFF << endl << DATA_ARFF << endl << endl;
    if (PRINT_OUT) 
    cout     << endl << DATA_ARFF << endl << endl;
  
  this->ligandCathFASTAProfile->ListCathDomainProfile()->SetInitial();
  while (this->ligandCathFASTAProfile->ListCathDomainProfile()->SetNext()) {

    String* currentSequence = NULL;
    
    if      (*typeOfData == "Seq") currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
    else if (*typeOfData == "CH")  currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassChemical();
    else if (*typeOfData == "PH")  currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassPhysical();
    else if (*typeOfData == "SS")  currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->SecondaryStructureP();
    else if (*typeOfData == "MT")  currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesMT();
    else                           currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
    
    this->searchPattern->Sequence(currentSequence);
    
    fileARFF << this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain()->Out();
    if (PRINT_OUT) 
      cout     << this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain()->Out();

    counter = 0;
    
    currentPratt2Patterns->Patterns()->SetInitial();
    while (currentPratt2Patterns->Patterns()->SetNext() && counter++ < numberAttributes) {
      
      this->searchPattern->Pattern(currentPratt2Patterns->Patterns()->GetCurrent()->Pattern());
      this->searchPattern->SearchPattern();
      
      fileARFF << ((this->searchPattern->Segments()->Length())?VALUE_YES_ARFF:VALUE_NO_ARFF);
      if (PRINT_OUT) 
        cout     << ((this->searchPattern->Segments()->Length())?VALUE_YES_ARFF:VALUE_NO_ARFF);

    }
    
    fileARFF << VALUE_BIND_ARFF << endl;
    if (PRINT_OUT) 
      cout     << VALUE_BIND_ARFF << endl;

  }
  
  int numberFalseData = (int) (this->ligandCathFASTAProfile->ListCathDomainProfile()->Length()*percentageFalse/(100 - percentageFalse));
  int counterFalse    = 0;
  
  TListE <int>* listPosition  = new TListE <int> ();
  int           numberElemDel = 0;
  
  numberElemDel = this->cathFASTAProfile->ListCathDomainProfile()->Length() - (numberFalseData + this->ligandCathFASTAProfile->ListCathDomainProfile()->Length());
  
  if (numberElemDel < 0) {
    
    fileARFF << "ERROR!" << endl;
    cout     << "ERROR!" << endl;
     
    fileARFF.close();
    
    if (listPosition) delete listPosition;
    
    return false;
    
  }
    
  for (long i = 0; i < this->cathFASTAProfile->ListCathDomainProfile()->Length(); i++) 
    listPosition->Add(new int (i + 1));
  
  for (long i = 0; i < numberElemDel; i++) 
    listPosition->Delete(rand() % listPosition->Length());
  
  listPosition->SetInitial();
  if (!listPosition->SetNext()) {
    
    fileARFF << "ERROR!" << endl;
    cout     << "ERROR!" << endl;
     
    fileARFF.close();
    
    if (listPosition) delete listPosition;
    
    return false;
    
  }
    
  long position = 0;
    
  this->cathFASTAProfile->ListCathDomainProfile()->SetInitial();
  while (this->cathFASTAProfile->ListCathDomainProfile()->SetNext() && counterFalse < numberFalseData) {

    if (*(listPosition->GetCurrent()) != ++position) continue;
    
    CathFASTAProfile* searchCathFASTAProfile = this->ligandCathFASTAProfile->ListCathDomainProfile()->FirstOcurrence(PatternSpace::EqualDomain, this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain());
    
    if (searchCathFASTAProfile) { 
      if (!listPosition->SetNext()) 
        break;
      continue;
    }
    
    if (this->excludeCathFASTAProfile && this->excludeCathFASTAProfile->ListCathDomainProfile()->Length()) {
      
      searchCathFASTAProfile = this->excludeCathFASTAProfile->ListCathDomainProfile()->FirstOcurrence(PatternSpace::EqualDomain, this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain());

      if (searchCathFASTAProfile) { 
        if (!listPosition->SetNext()) 
          break;
        continue;
      }
      
    }
    
    counterFalse++;
    
    String* currentSequence = NULL;
    
    if      (*typeOfData == "Seq") currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
    else if (*typeOfData == "CH")  currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassChemical();
    else if (*typeOfData == "PH")  currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassPhysical();
    else if (*typeOfData == "SS")  currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->SecondaryStructureP();
    else if (*typeOfData == "MT")  currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesMT();
    else                          currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
    
    this->searchPattern->Sequence(currentSequence);
    
    fileARFF << this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain()->Out();
    if (PRINT_OUT) 
      cout     << this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain()->Out();

    counter = 0;
    
    currentPratt2Patterns->Patterns()->SetInitial();
    while (currentPratt2Patterns->Patterns()->SetNext() && counter++ < numberAttributes) {
      
      this->searchPattern->Pattern(currentPratt2Patterns->Patterns()->GetCurrent()->Pattern());
      this->searchPattern->SearchPattern();
      
      fileARFF << ((this->searchPattern->Segments()->Length())?VALUE_YES_ARFF:VALUE_NO_ARFF);
      if (PRINT_OUT) 
        cout     << ((this->searchPattern->Segments()->Length())?VALUE_YES_ARFF:VALUE_NO_ARFF);

    }
    
    fileARFF << VALUE_NBIND_ARFF << endl;
    if (PRINT_OUT) 
      cout     << VALUE_NBIND_ARFF << endl;

    if (!listPosition->SetNext()) break;
    
  }
  
  fileARFF.close();
  
  if (listPosition) delete listPosition;
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PatternsCathFASTAProfile::CreateARFFBinary (String* nameRelation, String* comments, int positionInPatternFile, float percentagePattern, float percentageFalse, String* typeOfData) {
  
  if (!this->cathFASTAProfile || !this->ligandCathFASTAProfile || !this->prattPatterns || !this->filenameOut || !this->filenameOut->Length() || 
      (positionInPatternFile < 0) || (positionInPatternFile > this->prattPatterns->ListGroupPrattPatterns()->Length()))
    return false;
  
  ofstream fileARFF (this->filenameOut->Out());
  
  if (!fileARFF.is_open()) return false;
  
  char     buffer [LENGTHLINE + 1];
  char*    parts = NULL;
  
  
  strcpy (buffer, comments->Out());
  
  parts = strtok(buffer, "\n\r");
  while (parts) {
    fileARFF << COMMENT_ARFF << parts << endl;
    if (PRINT_OUT) 
      cout     << COMMENT_ARFF << parts << endl;
    parts = strtok(NULL, "\n\r");
  }
  
  fileARFF << endl << RELATION_ARFF << CUOTE_ARFF << nameRelation->Out() << CUOTE_ARFF << endl << endl;
  if (PRINT_OUT) 
    cout     << endl << RELATION_ARFF << CUOTE_ARFF << nameRelation->Out() << CUOTE_ARFF << endl << endl;
  
  int             numberAttributes      = (ATTRIBUTE_COMPLETE)?
                                          (int) (this->prattPatterns->ListGroupPrattPatterns()->Get(positionInPatternFile - 1)->Patterns()->Length()*percentagePattern/100):
                                          (int) (this->ligandCathFASTAProfile->ListCathDomainProfile()->Length()*percentagePattern/100);
  int             counter               = 0;
  
  fileARFF << DOMAIN_ATT_ARFF << endl;
  if (PRINT_OUT) 
    cout     << DOMAIN_ATT_ARFF << endl;
  
  Pratt2Patterns* currentPratt2Patterns = this->prattPatterns->ListGroupPrattPatterns()->Get(positionInPatternFile - 1);
  
  currentPratt2Patterns->Patterns()->SetInitial();
  while (currentPratt2Patterns->Patterns()->SetNext() && counter++ < numberAttributes) {
    sprintf(buffer, PATTERN_ATT_ARFF, currentPratt2Patterns->Patterns()->GetCurrent()->Pattern()->Out());
    fileARFF << buffer << endl;
    if (PRINT_OUT) 
      cout     << buffer << endl;
  }
  
  fileARFF << CLASS_ATT_ARFF << endl;
  if (PRINT_OUT) 
    cout     << CLASS_ATT_ARFF << endl;
  
  fileARFF << endl << DATA_ARFF << endl << endl;
  if (PRINT_OUT) 
    cout     << endl << DATA_ARFF << endl << endl;
  
  this->ligandCathFASTAProfile->ListCathDomainProfile()->SetInitial();
  while (this->ligandCathFASTAProfile->ListCathDomainProfile()->SetNext()) {

    String* currentSequence = NULL;
    
    if      (*typeOfData == "Seq") currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
    else if (*typeOfData == "CH")  currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassChemical();
    else if (*typeOfData == "PH")  currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassPhysical();
    else if (*typeOfData == "SS")  currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->SecondaryStructureP();
    else if (*typeOfData == "MT")  currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesMT();
    else                          currentSequence = this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
    
    this->searchPattern->Sequence(currentSequence);
    
    fileARFF << this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain()->Out();
    if (PRINT_OUT) 
      cout     << this->ligandCathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain()->Out();

    counter = 0;
    
    currentPratt2Patterns->Patterns()->SetInitial();
    while (currentPratt2Patterns->Patterns()->SetNext() && counter++ < numberAttributes) {
      
      this->searchPattern->Pattern(currentPratt2Patterns->Patterns()->GetCurrent()->Pattern());
      this->searchPattern->SearchPattern();
      
      fileARFF << "," << (this->searchPattern->Segments()->Length()?1:0);
      if (PRINT_OUT) 
        cout     << "," << (this->searchPattern->Segments()->Length()?1:0);

    }
    
    fileARFF << VALUE_BIND_ARFF << endl;
    if (PRINT_OUT) 
      cout     << VALUE_BIND_ARFF << endl;

  }
  
  int numberFalseData = (int) (this->ligandCathFASTAProfile->ListCathDomainProfile()->Length()*percentageFalse/(100 - percentageFalse));
  int counterFalse    = 0;
  
  TListE <int>* listPosition  = new TListE <int> ();
  int           numberElemDel = 0;
  
  numberElemDel = this->cathFASTAProfile->ListCathDomainProfile()->Length() - (numberFalseData + this->ligandCathFASTAProfile->ListCathDomainProfile()->Length());
  
  if (numberElemDel < 0) {
    
    fileARFF << "ERROR!" << endl;
    cout     << "ERROR!" << endl;
     
    fileARFF.close();
    
    if (listPosition) delete listPosition;
    
    return false;
    
  }
    
  for (long i = 0; i < this->cathFASTAProfile->ListCathDomainProfile()->Length(); i++) 
    listPosition->Add(new int (i + 1));
  
  for (long i = 0; i < numberElemDel; i++) 
    listPosition->Delete(rand() % listPosition->Length());
  
  listPosition->SetInitial();
  if (!listPosition->SetNext()) {
    
    fileARFF << "ERROR!" << endl;
    cout     << "ERROR!" << endl;
     
    fileARFF.close();
    
    if (listPosition) delete listPosition;
    
    return false;
    
  }
    
  long position = 0;
    
  this->cathFASTAProfile->ListCathDomainProfile()->SetInitial();
  while (this->cathFASTAProfile->ListCathDomainProfile()->SetNext() && counterFalse < numberFalseData) {

    if (*(listPosition->GetCurrent()) != ++position) continue;
    
    CathFASTAProfile* searchCathFASTAProfile = this->ligandCathFASTAProfile->ListCathDomainProfile()->FirstOcurrence(PatternSpace::EqualDomain, this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain());
    
    if (searchCathFASTAProfile) { 
      if (!listPosition->SetNext()) 
        break;
      continue;
    }
    
    if (this->excludeCathFASTAProfile && this->excludeCathFASTAProfile->ListCathDomainProfile()->Length()) {
      
      searchCathFASTAProfile = this->excludeCathFASTAProfile->ListCathDomainProfile()->FirstOcurrence(PatternSpace::EqualDomain, this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain());

      if (searchCathFASTAProfile) { 
        if (!listPosition->SetNext()) 
          break;
        continue;
      }
      
    }
    
    counterFalse++;
    
    String* currentSequence = NULL;
    
    if      (*typeOfData == "Seq") currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
    else if (*typeOfData == "CH")  currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassChemical();
    else if (*typeOfData == "PH")  currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesClassPhysical();
    else if (*typeOfData == "SS")  currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->SecondaryStructureP();
    else if (*typeOfData == "MT")  currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->ResiduesMT();
    else                          currentSequence = this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Sequence();
    
    this->searchPattern->Sequence(currentSequence);
    
    fileARFF << this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain()->Out();
    if (PRINT_OUT) 
      cout     << this->cathFASTAProfile->ListCathDomainProfile()->GetCurrent()->Domain()->Out();

    counter = 0;
    
    currentPratt2Patterns->Patterns()->SetInitial();
    while (currentPratt2Patterns->Patterns()->SetNext() && counter++ < numberAttributes) {
      
      this->searchPattern->Pattern(currentPratt2Patterns->Patterns()->GetCurrent()->Pattern());
      this->searchPattern->SearchPattern();
      
      fileARFF << "," << (this->searchPattern->Segments()->Length()?1:0);
      if (PRINT_OUT) 
        cout     << "," << (this->searchPattern->Segments()->Length()?1:0);

    }
    
    fileARFF << VALUE_NBIND_ARFF << endl;
    if (PRINT_OUT) 
      cout     << VALUE_NBIND_ARFF << endl;

    if (!listPosition->SetNext()) break;
    
  }
  
  fileARFF.close();
  
  if (listPosition) delete listPosition;
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PatternsCathFASTAProfile PatternsCathFASTAProfile::operator= (const PatternsCathFASTAProfile& patternsCathFASTAProfile) {
  
  this->filenameCathFASTAProfile->In(patternsCathFASTAProfile.filenameCathFASTAProfile);
  this->filenameLigandCathFASTAProfile->In(patternsCathFASTAProfile.filenameLigandCathFASTAProfile);
  this->filenameExcludeCathFASTAProfile->In(patternsCathFASTAProfile.filenameExcludeCathFASTAProfile);
  this->filenamePatternsCompact->In(patternsCathFASTAProfile.filenamePatternsCompact);
  this->filenameLigandFASTAProfile->In(patternsCathFASTAProfile.filenameLigandFASTAProfile);
  this->filenameSMI->In(patternsCathFASTAProfile.filenameSMI);
  this->filenameOut->In(patternsCathFASTAProfile.filenameOut);
  
  *(this->prattPatterns)           = *(patternsCathFASTAProfile.prattPatterns);
  *(this->cathFASTAProfile)        = *(patternsCathFASTAProfile.cathFASTAProfile);
  *(this->ligandCathFASTAProfile)  = *(patternsCathFASTAProfile.ligandCathFASTAProfile);
  *(this->excludeCathFASTAProfile) = *(patternsCathFASTAProfile.excludeCathFASTAProfile);
  *(this->searchPattern)           = *(patternsCathFASTAProfile.searchPattern);
  
  return *this;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
